<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Admin\Profession;
use Input;
class ProfessionController extends Controller
{
    //列表操作
    public function index(){
    	//获取数据
    	$data = Profession::orderBy('sort','desc') -> with('rel_protype') -> get();
    	//显示视图
    	return view('admin.profession.index',['data' => $data]);
    }

    //添加操作
    public function add(){
    	if(Input::method() == 'POST'){
    		//提交处理
    		//自动验证
    		//接收数据
    		$data = Input::all();
    		//写入数据表
    		$result = Profession::insert([
    				'pro_name'		=>	$data['pro_name'],
    				'protype_id'	=>	$data['protype_id'],
    				'cover_img'		=>	$data['cover_img'],
    				'banner_img'    =>	$data['banner_img'],
    				'carousel_img'  =>	$data['carousel_img'],
    				'description'	=>	$data['description'],
    				'view_count'	=>	$data['view_count'],
    				'created_at'	=>	date('Y-m-d H:i:s'),
    				'sort'			=>	$data['sort'],
    				'duration'		=>	$data['duration'],
    			]);
    		//返回
    		return $result ? '1' : '0';
    	}else{
    		//获取所属的分类
	    	$type = \App\Admin\Protype::get();
	    	//视图展示
	    	return view('admin.profession.add',['type' => $type]);
    	}
    }

    public function edit(){
        if(Input::method() == 'POST'){
            $data = Input::all();
            $result = Profession::where('id',$data['id']) -> update([
                'pro_name'		=>	$data['pro_name'],
                'protype_id'	=>	$data['protype_id'],
                'cover_img'		=>	$data['cover_img'],
                'cover_img'		=>	$data['cover_img'],
                'banner_img'    =>	$data['banner_img'],
                'carousel_img'  =>	$data['carousel_img'],
                'description'	=>	$data['description'],
                'view_count'	=>	$data['view_count'],
                'created_at'	=>	date('Y-m-d H:i:s'),
                'sort'			=>	$data['sort'],
                'duration'		=>	$data['duration'],
            ]);
            return $result ? '1' : '0';
        }else{
            $id = Input::get('id');
            $data = Profession::where('id',$id) -> first();
            $type = \App\Admin\Protype::get();
            return view('admin.profession.edit',['type' => $type , 'data' => $data]);
        }
    }

    public function del(){
        $id = Input::get('id');
        $res = Profession::where('id',$id) -> delete();
        $res ? $data['status'] = 200 :  $data['status'] = 0;
        return response() -> json($data);
    }

    public function search(){
        $keyword = Input::get('key');
        $start_time = Input::get('datemin') ? Input::get('datemin') . ' 00:00:00' : '1999-01-01 00:00:00';
        $end_time = Input::get('datemax')? Input::get('datemax') . ' 23:59:59' : date('Y-m-d H:i:s');
        $data = Profession::where('pro_name','LIKE',"%$keyword%") -> whereBetween('created_at',[$start_time,$end_time]) -> get();
        //展示视图，携带数据
        return view('admin.profession.index',['data' => $data, 'key' => $keyword ,'datemin' => Input::get('datemin') , 'datemax' => Input::get('datemax')]);
    }
}
